/*
 * Created by SharpDevelop.
 * User: Heikki
 * Date: 8.11.2008
 * Time: 23:42
 */

using System;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using CookComputing.XmlRpc;

namespace notebooklet
{
	public class Client
	{
		public static string version = "0.02";
		
		public Client()
		{
		}
		
		public string Send(String note) {
			HttpWebRequest sendRequest = (HttpWebRequest)WebRequest.Create("https://deggis.kapsi.fi/notebooklet/send.php");
			sendRequest.Method = "POST";
			sendRequest.ContentType = "text/xml";
			Stream streamSendRequest = (Stream) sendRequest.GetRequestStream();
	
			XmlTextWriter xmlSend = new XmlTextWriter(streamSendRequest, Encoding.UTF8);
			
			xmlSend.WriteStartDocument();
			xmlSend.WriteStartElement("methodCall");
				xmlSend.WriteElementString("methodName", "send");
				xmlSend.WriteStartElement("params");
					xmlSend.WriteStartElement("param");
						xmlSend.WriteElementString("value", note);
					xmlSend.WriteEndElement();
				xmlSend.WriteEndElement();
			xmlSend.WriteEndElement();

			xmlSend.Close();
					
			HttpWebResponse webResponseSend = (HttpWebResponse) sendRequest.GetResponse();
			
			StreamReader streamSendResponse = new StreamReader(webResponseSend.GetResponseStream());

			string result = "";
			
			XmlTextReader xmlResult = new XmlTextReader(streamSendResponse);
			while(xmlResult.Read()) {
				XmlNodeType nType = xmlResult.NodeType;
                if (nType == XmlNodeType.Element)
                {
                    if(xmlResult.Name.ToString() == "string") {
                    	result = xmlResult.ReadElementString();
                    }
                }
			}
			
			streamSendResponse.Close();
      		webResponseSend.Close();
      		
      		return result;
		}
	}
}
